require(['/js/config.js'],()=>{
    require(['template','request','jquery','header','footer'],(template,req)=>{
        class cartPage{
            constructor(){
                this.init()
                this.calcsum()
                this.selectAll()
            }
            init(){
                this.getData()
                this.payFor()
            }
            getData(){
                this.list = JSON.parse(localStorage.getItem("info"))
                console.log("GET调用",this.list)
                this.BrashData()
                this.select()
                this.delete()
            }
            BrashData(){
                $(".tbody").html(template("cartBrash",{list:this.list}))
            }
            calcsum(){
                this.list = JSON.parse(localStorage.getItem("info"))
                console.log("zongjia调用",this.list)
                let su = 0,
                num = 0
                this.list.forEach((item)=>{
                    if(item.checked){
                        su += item.price*item.count
                        num += item.count
                    }
                })
                let sum =su.toFixed(2)
                $(".sumBack").html(template("sumBrash",{res:this.list,sum,num}))
            }
            select(){
                // $(".selectOne").every(arm=>{
                //     return $(item).prop("checked")
                // })
                // $(".selectAll").
                const _this = this
                $(".selectOne").on("change",function(){
                    let id = Number($(this).parents("ul").attr("index"))
                    
                    _this.list =_this.list.map(item=>{
                        if(id == item.id){
                            item.checked = $(this).prop("checked")
                        }
                        return item
                    })
                    localStorage.setItem('info',JSON.stringify(_this.list))
                    _this.calcsum()
                    _this.selectAll()
                })

            }
            selectAll(){
                let sta= this.list.every(item=>{
                    return item.checked
                })
                $('.selectAll').prop("checked",sta)
            }
            delete(){
                const _this = this
                $(".deleteBtn").on("click",function(e){
                    let id = Number($(this).parents("ul").attr("index"))
                    _this.list =_this.list.map(item=>{
                        if(id == item.id){
                            _this.list.splice($(e.target).parent().index(),1)
                            localStorage.setItem('info',JSON.stringify(_this.list))
                        }
                        return item
                    })
                    _this.calcsum()
                    _this.getData()
                })
        }
            payFor(){
                $(".counts").on("click",()=>{
                    $(".alert").fadeIn(400,()=>{
                        $(".alert").delay(700).fadeOut(700,()=>{

                        })
                    })
                })
            }
        }
        return  new cartPage()
    })
})